package com.lyk.coursearrange.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyk.coursearrange.common.core.base.code.CodeEnum;
import com.lyk.coursearrange.common.exception.MZException;
import com.lyk.coursearrange.dao.CustomerInformationDao;
import com.lyk.coursearrange.dao.SourceDao;
import com.lyk.coursearrange.dao.SourcePeopleDao;
import com.lyk.coursearrange.entity.CustomerInformation;
import com.lyk.coursearrange.entity.Sale;
import com.lyk.coursearrange.entity.Stages;
import com.lyk.coursearrange.entity.dto.CustomerInformationDto;
import com.lyk.coursearrange.entity.dto.CustomerInformationDtoTwo;
import com.lyk.coursearrange.excel.ExcelCustomerInformationOutData;
import com.lyk.coursearrange.service.CustomerInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
public class CustomerInformationServiceImpl extends ServiceImpl<CustomerInformationDao, CustomerInformation> implements CustomerInformationService {

    @Autowired
    private CustomerInformationDao customerInformationDao;
    @Autowired
    private SourceDao sourceDao;
    @Autowired
    private SourcePeopleDao sourcePeopleDao;



    String admin_no = null;


    @Override
    public CustomerInformation getById(Long id) {
        return customerInformationDao.getById(id);
    }

    @Override
    public List<CustomerInformation> customerinformationlist(Map<String,String> param) {

        Long page = 1L;
        Long limit = 100000000000000L;
        String customer_student =null;
        String customer_account = null;
        String customer_wx =null;
        Long customer_state = 0L;
        Long customer_type =0L;
        Long customer_isactive =0L;
        Long sourcepeople_id = 0L;

        Integer type = null;

        if(param.get("customerIsactive")!=null&&!param.get("customerIsactive").equals("")){
            customer_isactive = Long.valueOf(param.get("customerIsactive"));
        }
        if(param.get("customerType")!=null&&!param.get("customerType").equals("")){
            customer_type = Long.valueOf(param.get("customerType"));
        }
        if(param.get("page")!=null&&!param.get("page").equals("")){
            page = Long.valueOf(param.get("page"));
        }
        if(param.get("limit")!=null&&!param.get("limit").equals("")){
            limit = Long.valueOf(param.get("limit"));
        }
        if(param.get("customerStudent")!=null&&!param.get("customerStudent").equals("")){
            customer_student = param.get("customerStudent");
        }
        if(param.get("customerWx")!=null&&!param.get("customerWx").equals("")){
            customer_wx = param.get("customerWx");
        }
        if(param.get("customerAccount")!=null&&!param.get("customerAccount").equals("")){
            customer_account = param.get("customerAccount");
            Long user_type = customerInformationDao.getuser_type(customer_account);
            if(user_type==2) {
                sourcepeople_id = customerInformationDao.getsourcepeople_id(customer_account);
            }else if(user_type==0){
                customer_account =null;
            }
        }
        if(param.get("customerState")!=null&&!param.get("customerState").equals("")){
            customer_state = Long.valueOf(param.get("customerState"));
        }
        if(param.get("type")!=null&&!param.get("type").equals("")){
            type = Integer.valueOf(param.get("type"));
        }
        Long offset = (page - 1) * limit;
        if(sourcepeople_id!=0){
            return customerInformationDao.customerinformationlisttwo(customer_student,offset,limit,sourcepeople_id,customer_wx,customer_state,customer_type,customer_isactive,type);
        }
        return customerInformationDao.customerinformationlist(customer_student,offset,limit,customer_account,customer_wx,customer_state,customer_type,customer_isactive,type);
    }

    @Override
    public String getsourcename(Long customerId) {
        return customerInformationDao.getsourcename(customerId);
    }

    @Override
    public boolean customerinformationadd(Map<String, String> param) {


        //Long sub_account = customerInformationDao.selectsequence();
      /*  Long a = customerInformationDao.selectcounta();
        Long b = customerInformationDao.selectcountb();*/

        String WX = param.get("customerWx");
        String customerWx = param.get("customerWx");
        String customer_url  = param.get("customer_url");
        String customer_ewm = "没有选择添加的二维码图片" ;
        Long customer_type = Long.valueOf(param.get("customerType"));
/*        Long customer_isactive = Long.valueOf(param.get("customerIsactive"));*/
        Long customer_isactive =1L;
        if(param.get("customerIsactive")!=null&&!param.get("customerIsactive").equals("")){
            customer_isactive = Long.valueOf(param.get("customerIsactive"));
        }
        System.out.println("这个类型到底是？"+customer_type);
        if(customer_type==2) {
           Long.valueOf(IdUtil.getSnowflake().nextIdStr());
            WX = IdUtil.getSnowflake().nextIdStr();
            System.out.println("雪花算法生成的不重复随机微信号:"+customerWx);
            customer_ewm = param.get("customer_ewm");
        }
        if(selectWX(WX)>0){
            throw  new MZException(CodeEnum.WXEXIT);
        }
        CustomerInformation customerInformation = CustomerInformation.builder()
                .customerWx(customerWx)
                .customerSource(Long.valueOf(param.get("customerSource")))
                .customerSourcepeople(Long.valueOf(param.get("customerSourcepeople")))
                .customerUrl("http://mzkt.top/img/"+customer_url)
                .customerTeacherid(Long.valueOf(param.get("customerTeachername")))
                .customerEwm("http://mzkt.top/ewm/"+customer_ewm)
                .customerType(customer_type)
                .customerIsactive(customer_isactive)
                .build();
        //防止从新部署服务器admin_no直接清空
        if(admin_no==null){
            admin_no = customerInformationDao.selectmax();
        }

        //分配账号算法
        //每添加10条去查询账号最少的然后往站好最少得里面添加
     /*   if(sub_account%10==0){*/
            //账号的唯一编号
           admin_no = customerInformationDao.selectsmall();
            Long order_number = customerInformationDao.selectorder_number(admin_no);
            Long b = order_number +1;
            customerInformationDao.updatealgorithm(admin_no,b);
       /* }*/
        System.out.println("看一下是被动还是主动"+customer_isactive);
        if(customer_isactive==2){
            if(param.get("adminNo")!=null&&!param.get("adminNo").equals("")){
                admin_no = param.get("adminNo");
            }
        }

        customerInformation.setCustomerAccount(admin_no);



     /*   customerInformation.setCustomerAccount("杨老师");*/
      /*  if (a%10==0&&a>b){
            customerInformation.setCustomerAccount("苏老师");
        }else if (a%10==0&&b>a){
            customerInformation.setCustomerAccount("杨老师");
        }*/
        return customerInformationDao.customerinformationadd(customerInformation);
    }

    @Override
    public boolean customerstateupdate(Map<String, String> param) {
        UpdateWrapper<CustomerInformation> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("customer_state",Long.valueOf(param.get("customerState")));
        updateWrapper.eq("customer_id",Long.valueOf(param.get("customerId")));
        return update(updateWrapper);
    }

    @Override
    public int customerdelete(Long customer_id) {
        QueryWrapper<CustomerInformation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id",customer_id);
        return customerInformationDao.delete(queryWrapper);
    }

    @Override
    public String getsourcepeoplename(Long customerSourcepeople) {
        return sourcePeopleDao.getsourcepeoplename(customerSourcepeople);
    }

    @Override
    public boolean customerstudentupdate(CustomerInformationDtoTwo c) {
        Long customerIspay = 0L;
        if(c.getCustomerIspay()!=null&&!c.getCustomerIspay().equals("")){
            customerIspay = Long.valueOf(c.getCustomerIspay());
        }
        Long  customerIsallpay = 0L;
        if(c.getCustomerIsallpay()!=null&&!c.getCustomerIsallpay().equals("")){
            customerIsallpay = Long.valueOf(c.getCustomerIsallpay());
        }
        UpdateWrapper<CustomerInformation> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("customer_sourcepeople",Long.valueOf(c.getCustomerSourcepeople()));
        updateWrapper.set("customer_state",Long.valueOf(c.getCustomerState()));
        updateWrapper.set("customer_sale",Long.valueOf(c.getCustomerSale()));
        updateWrapper.set("customer_student",c.getCustomerStudent());
        updateWrapper.set("customer_ispay",customerIspay);
        updateWrapper.set("customer_isallpay",  customerIsallpay);
        updateWrapper.set("pay_type",  c.getPayType());
        if(customerIspay==1){
            updateWrapper.set("customer_paymoney",c.getCustomerPaymoney());
            updateWrapper.set("customer_paytime",c.getCustomerPaytime());
        }else {
            updateWrapper.set("customer_paymoney",null);
            updateWrapper.set("customer_paytime",null);
        }
        if(customerIsallpay==2){
            updateWrapper.set("customer_allpaymoney",null);
            updateWrapper.set("customer_allpaytime", null);
            updateWrapper.set("pay_alltype",  null);
            //先删数据
            sourcePeopleDao.deletebycustomeridone(c.getCustomerId());
            //再删关系
            sourcePeopleDao.deletebycustomeridtwo(c.getCustomerId());
            List<Stages> stagesList = c.getStages();
            stagesList.forEach(stages -> {
                boolean a = sourcePeopleDao.insertstages(stages);
                if (a){
                    sourcePeopleDao.insertcustomerasstages(c.getCustomerId(),stages.getStagesId());
                }else {
                    System.out.println("添加失败");
                }
            });
        }else {
            //先删数据
            sourcePeopleDao.deletebycustomeridone(c.getCustomerId());
            //再删关系
            sourcePeopleDao.deletebycustomeridtwo(c.getCustomerId());
            updateWrapper.set("customer_allpaymoney",c.getCustomerAllpaymoney());
            updateWrapper.set("customer_allpaytime", c.getCustomerAllpaytime());
            updateWrapper.set("pay_alltype",  c.getPayAlltype());
        }
        updateWrapper.eq("customer_id",Long.valueOf(c.getCustomerId()));
        return update(updateWrapper);
    }
    @Override
    public boolean customerisactiveupdate(Map<String, String> param) {
        UpdateWrapper<CustomerInformation> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("customer_isactive",param.get("customerIsactive"));
        updateWrapper.eq("customer_id",Long.valueOf(param.get("customerId")));
        return update(updateWrapper);
    }

    @Override
    public List<Sale> customersale() {
        return customerInformationDao.customersale();
    }


    @Override
    public CustomerInformationDtoTwo customersone(Long customer_id) {
        CustomerInformationDtoTwo c = customerInformationDao.customersone(customer_id);
        c.setStages(sourcePeopleDao.selectstages(customer_id));
        return c;
    }

    @Override
    public int selectWX(String wx) {
        return customerInformationDao.selectWX(wx);
    }

    @Override
    public Long customerinformationcount(Map<String, String> param) {

        String customer_student =null;
        String customer_account = null;
        String customer_wx =null;
        Long customer_state = 0L;
        Long customer_type =0L;
        Long customer_isactive =0L;
        Long sourcepeople_id = 0L;
        Integer type = null;
        if(param.get("customerIsactive")!=null&&!param.get("customerIsactive").equals("")){
            customer_isactive = Long.valueOf(param.get("customerIsactive"));
        }
        if(param.get("customerType")!=null&&!param.get("customerType").equals("")){
            customer_type = Long.valueOf(param.get("customerType"));
        }
        if(param.get("customerStudent")!=null&&!param.get("customerStudent").equals("")){
            customer_student = param.get("customerStudent");
        }
        if(param.get("customerWx")!=null&&!param.get("customerWx").equals("")){
            customer_wx = param.get("customerWx");
        }
        if(param.get("customerAccount")!=null&&!param.get("customerAccount").equals("")){
            customer_account = param.get("customerAccount");
            Long user_type = customerInformationDao.getuser_type(customer_account);
            if(user_type==2) {
                sourcepeople_id = customerInformationDao.getsourcepeople_id(customer_account);
            }else if(user_type==0){
                customer_account =null;
            }
        }
        if(param.get("customerState")!=null&&!param.get("customerState").equals("")){
            customer_state = Long.valueOf(param.get("customerState"));
        }
        if(param.get("type")!=null&&!param.get("type").equals("")){
            type = Integer.valueOf(param.get("type"));
        }
        if(sourcepeople_id!=0){
            return customerInformationDao.customerinformationcounttwo(customer_student,sourcepeople_id,customer_wx,customer_state,customer_type,customer_isactive,type);
        }
        return customerInformationDao.customerinformationcount(customer_student,customer_account,customer_wx,customer_state,customer_type,customer_isactive,type);
    }




}
